
import { db } from '@/storage'
import { appConfig, settingConfig } from '@/config'
import router from '@/router'

export const handlerRequstConfig = (config) => {
    const { key, value } = appConfig.token
    const token = db.get('token')
    const tokenKeys = {}
    if (key) {
        tokenKeys[key] = value.split(' ')[0] + ' ' + token
    }
    let _header = {}
    if (token) {
        _header = {
            ..._header,
            ...tokenKeys
        }
    }

    // 查询是否超时
    const time = db.get('expires')
    if (new Date().getTime() - time > appConfig.token?.expires) {
        router.push({
            name: settingConfig.LoginMainLayoutName
        })
    }

    return { ...config, headers: { ...config.headers, ..._header } }
}
